package com.cw1.protoType.deepclone;


import java.io.*;

/**
 * @Author qiuMi
 * @description 深拷贝 - 1重写clone方法
 * @Date 2022-10-17 21:52
 **/
public class DeepProtoType1 implements Serializable, Cloneable {

    static final long serialVersionUID = 66953837908477363L;

    public String name;
    public DeepCloneableTarget deepCloneableTarget;

    public DeepProtoType1() {
        super();
    }

    /**
     * 深拷贝 -- 方法一 重写clone方法
     *
     * @return 深拷贝的方法一
     * @throws CloneNotSupportedException
     */
    @Override
    protected Object clone() throws CloneNotSupportedException {
        Object deep = null;
        // 这里对类中的基本属性进行处理
        deep = super.clone();
        // 这里对类中的引用数据类型进行处理
        DeepProtoType1 deepProtoType1 = (DeepProtoType1) deep;
        deepProtoType1.deepCloneableTarget = (DeepCloneableTarget) deepCloneableTarget.clone();
        return deepProtoType1;
    }

    /**
     * 深拷贝 - 方式二 通过对象的序列化实现
     *
     * @return 拷贝
     */
    public Object deepClone() {
        ByteArrayOutputStream byteOutputStream = null;
        ByteArrayInputStream byteInputStream = null;
        ObjectInputStream objectInputStream = null;
        ObjectOutputStream objectOutputStream = null;
        try {
            // 序列化
            byteOutputStream = new ByteArrayOutputStream();
            objectOutputStream = new ObjectOutputStream(byteOutputStream);
            objectOutputStream.writeObject(this);

            // 反序列化
            byteInputStream = new ByteArrayInputStream(byteOutputStream.toByteArray());
            objectInputStream = new ObjectInputStream(byteInputStream);
            DeepProtoType1 deep = (DeepProtoType1) objectInputStream.readObject();
            return deep;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                byteOutputStream.close();
                byteInputStream.close();
                objectInputStream.close();
                objectOutputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 深拷贝 -- 序列化
     *
     * @return deep
     */
    private Object deepClone2() {
        ByteArrayOutputStream byteArrayOutputStream = null;
        ByteArrayInputStream byteArrayInputStream = null;
        ObjectOutputStream objectOutputStream = null;
        ObjectInputStream objectInputStream = null;
        try {
            // 序列化
            byteArrayOutputStream = new ByteArrayOutputStream();
            objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(this);

            // 反序列化
            byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            objectInputStream = new ObjectInputStream(byteArrayInputStream);
            DeepProtoType1 deepProtoType1 = (DeepProtoType1) objectInputStream.readObject();
            return deepProtoType1;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                byteArrayOutputStream.close();
                byteArrayInputStream.close();
                objectOutputStream.close();
                objectInputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
